/*
 * Copyright (c) 2008, Xiong Yingfei, the University of Tokyo All rights
 * reserved.
 */
package ac.jp.u_tokyo.SyncLib.dictionaries;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import sun.security.krb5.internal.crypto.k;
import sun.security.krb5.internal.crypto.v;

import ac.jp.u_tokyo.SyncLib.Mod;
import ac.jp.u_tokyo.SyncLib.NullMod;
import ac.jp.u_tokyo.SyncLib.PrimMod;
import ac.jp.u_tokyo.SyncLib.util.Helper;

public class DictModFactory<K, V>
{

//	private LinkedList<DictMod.InnerMod<K, V>> list = new LinkedList<DictMod.InnerMod<K, V>>();
	
	private Map<K, Mod<V>> mods = new HashMap<K, Mod<V>>();

	
//	public void add(DictMod.InnerMod<K, V> inner)
//	{
//		if (inner != null)
//			list.add(inner);
//	}
	
	public void addPut(K key, Mod<V> mod) {
		if (mod instanceof NullMod) return;
		mods.put(key, Helper.merge(mods.get(key), mod));
	}
	
	public void addRemove(K key) {
		addPut(key, PrimMod.NULL);
	}

	public DictMod<K, V> create()
	{
//		if (mods.size() == 0)  return NullMod.INSTANCE;
		return new DictMod<K, V>(mods);
	}
	
	public static <K, V> DictMod<K, V> creatvPut(K key, Mod<V> mod) {
		DictModFactory<K, V> factory = new DictModFactory<K, V>();
		factory.addPut(key, mod);
		return factory.create();
	}

	public static <K, V> DictMod<K, V> creatvRemove(K key) {
		DictModFactory<K, V> factory = new DictModFactory<K, V>();
		factory.addRemove(key);
		return factory.create();
	}

//	public static <K, V> DictMod.InnerMod<K, V> createRemove(K key){
//		return new DictMod.RemoveMod<K, V>(key);
//	}
//
//	public static <K, V> DictMod.InnerMod<K, V> createPut(K key, Mod<V> mod){
//		if (mod.equals(PrimMod.NULL)) return createRemove(key);
//		return new DictMod.PutMod<K, V>(key, mod);
//	}

}
